<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-17 17:30:38
 * @Last Modified time: 2023-02-21 16:48:35
 */
namespace app\common\model\store;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use app\common\enum\EnumFactory;
use frank\utils\helper\ArrayHelper;
use app\common\enum\store\DeliveryTypeEnum;

/**
 * 商城-售后单
 * Class OrderRefundModel
 * @package app\common\model\store
 */
class OrderRefundModel extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'store_order_refund';

    /**
     * 一对一关联用户表 (简单信息)
     * @return \think\model\relation\HasOne
     */
    public function user()
    {
        return $this->hasOne("app\\common\\model\\user\\UserModel", 'id', 'user_id')->field(['id','nickName', 'avatar'])->bind([
            'user_nickName' => 'nickName',
            'user_avatar' => 'avatar'
        ]);
    }

    /**
     * 关联订单主表
     * @return \think\model\relation\HasOne
     */
    public function order()
    {
        return $this->HasOne("app\\common\\model\\store\\OrderModel",'id','order_id');
    }

    /**
     * 关联订单商品表
     * @return \think\model\relation\BelongsTo
     */
    public function orderGoods()
    {
        return $this->belongsTo("app\\common\\model\\store\\OrderGoodsModel",'order_goods_id');
    }

    /**
     * 关联退货地址表
     * @return \think\model\relation\HasOne
     */
    public function address()
    {
        return $this->hasOne("app\\common\\model\\store\\RefundAddressModel",'id','refund_address_id');
    }

    /**
     * 关联物流公司表
     * @return \think\model\relation\BelongsTo
     */
    public function userExpress()
    {
        return $this->hasOne("app\\common\\model\\store\\ExpressCompanyModel",'id','user_express_id')->bind([
            'user_express_company_name' => 'company_name',
        ]);
    }

    /**
     * 关联物流公司表
     * @return \think\model\relation\BelongsTo
     */
    public function express()
    {
        return $this->hasOne("app\\common\\model\\store\\ExpressCompanyModel",'id','express_id')->bind([
            'express_company_name' => 'company_name',
        ]);
    }

    /**
     * 类型修改器
     */
    public function getTypeAttr($value)
    {
        return EnumFactory::instance('store.order.refund_type')->getItem($value);
    }

    /**
     * 状态修改器
     */
    public function getStatusAttr($value,$data)
    {
        $status = EnumFactory::instance('store.order.refund_status')->getItem($value);
        if($value == 3 && $data['type'] != 3){
            if (!$data['user_send_status']) {
                $status['name'] = $data['delivery_type'] == DeliveryTypeEnum::EXPRESS?'待用户发货':'待用户到店'; 
            }
            if ($data['user_send_status'] == 1 && !$data['receipt_status']) {
                $status['name'] = '用户已货，待商家收货'; 
            }
            if ($data['type'] == 2 && $data['receipt_status'] == 1 && !$data['delivery_status']) {
                $status['name'] = '已商家收货，待商家重新发货'; 
            }
            if ($data['type'] == 2 && $data['delivery_status'] == 1 && !$data['user_receipt_status']) {
                $status['name'] = '商家已重新发货，待用户收货'; 
            }
        }
        return $status;
    }

    /**
     * 用户发货时间修改器
     */
    public function getUserSendTimeAttr($value)
    {
        return formatDatetime($value);
    }

    /**
     * 发货时间修改器
     */
    public function getSendTimeAttr($value)
    {
        return formatDatetime($value);
    }

    /**
     * 退款时间修改器
     */
    public function getRefundTimeAttr($value)
    {
        return formatDatetime($value);
    }

    /**
     * 配送类型修改器
     */
    public function getDeliveryTypeAttr($value)
    {
        return EnumFactory::instance('store.delivery_type')->getItem($value);
    }

    /**
     * 商品图片获取器
     */
    public function getImgsAttr($value){
        if(empty($value)) return [];
        $arr = explode(',', $value);
        foreach ($arr as $key => &$item) {
            $item = baseUrl().'/'.$item;
        }
        return $arr;
    }

    /**
     * id搜索器
     */
    public function searchIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('id', $value);
        } else {
            $query->where('id', $value);
        }
    }

    /**
     * user_id搜索器
     */
    public function searchUserIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('user_id', $value);
        } else {
            $query->where('user_id', $value);
        }
    }

    /**
     * status搜索器
     */
    public function searchStatusAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('status', $value);
        } else {
            $query->where('status', $value);
        }
    }
}


